package com.jianguo.web.controller.api;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jianguo.common.core.controller.BaseController;
import com.jianguo.common.core.domain.AjaxResult;
import com.jianguo.companion.domain.CompanionInfo;
import com.jianguo.companion.domain.PzCompanion;
import com.jianguo.companion.service.IPzCompanionService;
import com.jianguo.order.domain.PzOrder;
import com.jianguo.order.service.IPzOrderService;
import com.jianguo.user.domain.PzBalance;
import com.jianguo.user.domain.PzMoney;
import com.jianguo.user.domain.PzUser;
import com.jianguo.user.service.IPzBalanceService;
import com.jianguo.user.service.IPzMoneyService;
import com.jianguo.web.controller.tool.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

/**
 * @author hxq
 * @date 2024/03/15
 */
@RestController
@RequestMapping("/api/companion")
public class CompanionApi extends BaseController {
    @Autowired
    private IPzCompanionService pzCompanionService;

    @Autowired
    private IPzBalanceService pzBalanceService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private IPzMoneyService pzMoneyService;

    @Autowired
    private IPzOrderService pzOrderService;
    /**
     * 登录
     * @param pzCompanion
     * @return
     */
    @PostMapping(value = "/login")
    public AjaxResult companionLogin(@RequestBody PzCompanion pzCompanion) {
        LambdaQueryWrapper<PzCompanion> queryWrapper = new LambdaQueryWrapper<PzCompanion>()
                .eq(PzCompanion::getPhone, pzCompanion.getPhone())
                .eq(PzCompanion::getPassword, pzCompanion.getPassword());
        PzCompanion companion = pzCompanionService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(companion)){
            return AjaxResult.error("账号或密码错误");
        }
        return success();
    }

    /**
     * 获取用户详细信息
     * @param phone
     * @return
     */
    @GetMapping(value = "/companionInfo")
    public AjaxResult getcompanionInfo(String phone){
        if (ObjectUtil.isEmpty(phone)){
            return error();
        }
        //获取用户名以及手机号码
        LambdaQueryWrapper<PzCompanion> queryWrapper = new LambdaQueryWrapper<PzCompanion>()
                .eq(PzCompanion::getPhone, phone);
        PzCompanion companion = pzCompanionService.getOne(queryWrapper);

       CompanionInfo companionInfo = BeanUtil.copyProperties(companion, CompanionInfo.class);

       //  获取余额
        PzBalance balance = pzBalanceService.getOne(new LambdaQueryWrapper<PzBalance>().
                eq(PzBalance::getUserid, companion.getId()));
        // 设置余额
        companionInfo.setBalance(balance.getBalance());

        //获取收入
//        PzMoney money=pzMoneyService.getOne(new LambdaQueryWrapper<PzMoney>().
//                eq(PzMoney::getCompanionid,companion.getId()));
//        companionInfo.setMoney(money.getMoney());
////        companionInfo.setPassword("****");



        return success(companionInfo);
    }
    //验证码登录生成验证码
    @GetMapping(value = "/generateCode")
    public AjaxResult generateCode(String mobilePhone) {
        if (ObjectUtil.isEmpty(mobilePhone)) {
            return error();
        }
        //判断账号是否存在
        LambdaQueryWrapper<PzCompanion> queryWrapper = new LambdaQueryWrapper<PzCompanion>()
                .eq(PzCompanion::getPhone, mobilePhone);
        PzCompanion companion = pzCompanionService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(companion)){
            return AjaxResult.error("账号不存在");
        }
        // 随机生成
        String code = RandomUtil.randomNumbers(6);    // 随机生成6位数字验证码

        // 保存验证码到redis
        redisTemplate.opsForValue().set(mobilePhone, code, 5, TimeUnit.MINUTES);

        return success(code);
    }

    //注册生成验证码
    @GetMapping(value = "/generateCode1")
    public AjaxResult generateCode1(String mobilePhone) {
        if (ObjectUtil.isEmpty(mobilePhone)) {
            return error();
        }

        // 随机生成
        String code = RandomUtil.randomNumbers(6);    // 随机生成6位数字验证码

        // 保存验证码到redis
        redisTemplate.opsForValue().set(mobilePhone, code, 5, TimeUnit.MINUTES);

        return success(code);
    }

    //注册
    @PostMapping(value = "/register")
    public AjaxResult register(@RequestBody PzCompanion pzCompanion) throws NoSuchAlgorithmException {
        // 校验账号是否存在
        LambdaQueryWrapper<PzCompanion> queryWrapper = new LambdaQueryWrapper<PzCompanion>()
                .eq(PzCompanion::getPhone, pzCompanion.getPhone());
        PzCompanion companion = pzCompanionService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(companion)){
            return AjaxResult.error("账号已存在");
        }
        // 保存用户
        pzCompanion.setStatus(0L).setUsername(pzCompanion.getPhone());
        String password = pzCompanion.getPassword();
        pzCompanionService.save(pzCompanion);
        // 保存余额
        PzBalance pzBalance = new PzBalance();
        pzBalance.setUserid(pzCompanion.getId());
        pzBalance.setBalance(0L);
        pzBalanceService.save(pzBalance);
        return success();
    }

    //验证码登陆
    @GetMapping(value = "/codeLogin")
    public AjaxResult codeLogin(String mobilePhone, String code) {
        // 验证码校验
        String codeRedis = redisTemplate.opsForValue().get(mobilePhone);
        if (ObjectUtil.isEmpty(codeRedis) || !code.equals(codeRedis)){
            return AjaxResult.error("验证码错误");
        }

        // 登录成功
        return success();
    }

}
